//
//  ViewController.m
//  多选Cell
//
//  Created by My Mac on 2017/5/22.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) NSMutableArray *fixMarr;
@property(nonatomic,strong) NSMutableArray *selectMarr;
@property(nonatomic,strong) UIBarButtonItem *editItem;
@property(nonatomic,strong) UIBarButtonItem *allSelect;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewWithData];
}
-(void) initViewWithData
{
    self.navigationItem.title = @"多选删除";
    _fixMarr = [NSMutableArray new];
    _selectMarr = [NSMutableArray new];
    for (int i=0; i<10; i++) {
        [_fixMarr addObject:[NSString stringWithFormat:@"Item %d",i + 1]];
    }
    
    _editItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem = _editItem;
    _allSelect = [[UIBarButtonItem alloc]initWithTitle:@"全选" style:UIBarButtonItemStyleDone target:self action:@selector(selectAllCell)];
    _myTableView.tableFooterView = [UIView new];
}
-(void) selectAllCell
{
    [_selectMarr removeAllObjects];
    [_selectMarr addObjectsFromArray:_fixMarr];
    for (int i=0; i<_selectMarr.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [_myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    _allSelect.title = @"全不选";
    [_allSelect setAction:@selector(deSelectAllCell)];
}
-(void) deSelectAllCell
{
    for (int i=0; i<_selectMarr.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [_myTableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    [_selectMarr removeAllObjects];
    _allSelect.title = @"全选";
    [_allSelect setAction:@selector(selectAllCell)];
}
-(void)edit
{
    self.navigationItem.leftBarButtonItem = _allSelect;
    //改变_editItem标题
    _editItem.title = @"确定";
    //出现多选圈
    [_myTableView setEditing:YES animated:YES];
    //为确定绑定响应事件
    [_editItem setAction:@selector(sureDelect)];
}
-(void)sureDelect
{
    if (_selectMarr.count >0) {
        [_selectMarr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isEqualToString:_selectMarr[idx]]) {
                [_fixMarr removeObject:obj];
            }
        }];
        [_selectMarr removeAllObjects];
        [_myTableView reloadData];
        
        _editItem.title = @"删除";
        [_myTableView setEditing:NO animated:YES];
        [_editItem setAction:@selector(edit)];
        self.navigationItem.leftBarButtonItem = nil;
    } else {
        [self setAlert];
    }
}
-(void)setAlert
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您未选择任何一项" preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        _editItem.title = @"删除";
        self.navigationItem.leftBarButtonItem = nil;
        [_myTableView setEditing:NO animated:YES];
        [_editItem setAction:@selector(edit)];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"继续选择" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_fixMarr.count ==0) {
        _label.hidden = NO;
        [_editItem setEnabled:NO];
    }
    return _fixMarr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell];
    }
    cell.textLabel.text = _fixMarr[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
//cell的编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//选择
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_editItem.title isEqualToString:@"确定"]){
        [_selectMarr addObject:[_fixMarr objectAtIndex:indexPath.row]];
        if ([_allSelect.title isEqualToString:@"全选"]) {
            if (_selectMarr.count == _fixMarr.count) {
                _allSelect.title = @"全不选";
                [_allSelect setAction:@selector(deSelectAllCell)];

            }
        }
    }
}
//取消选择
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_editItem.title isEqualToString:@"确定"]){
        [_selectMarr removeObject:[_fixMarr objectAtIndex:indexPath.row]];
        if ([_allSelect.title isEqualToString:@"全不选"]) {
            if (_selectMarr.count <_fixMarr.count) {
                _allSelect.title = @"全选";
                [_allSelect setAction:@selector(selectAllCell)];
            }
        }
    }
}
@end
